/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pb168.eis_web;

import cz.muni.fi.pb138.eis_core.ArtworkEntity;
import cz.muni.fi.pb138.eis_core.ArtworkManager;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.xquery.XQException;

/**
 *
 * @author Jakub Senko
 */
@WebServlet(name = "CRUDServlet", urlPatterns =
{
    "/view", "/insert", "/delete"
})
public class CRUDServlet extends HttpServlet
{

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, XQException
    {
        response.setContentType("text/html;charset=UTF-8");
        
                String servletPath = request.getServletPath();
        RequestDispatcher dispatcher = null;
        String nextPage = null;

        ArtworkManager am = (ArtworkManager) getServletContext().getAttribute("artworkManager");

        if (servletPath.equals("/view"))
        {
            nextPage = "/frontend/view.jsp";
            request.setAttribute("artwork", am.getById(Long.parseLong(request.getParameter("id"))));

        

        } else if (servletPath.equals("/insert")) {

            request.setCharacterEncoding("utf-8");
            
            String name = request.getParameter("name");
            String author = request.getParameter("author");
            String description = request.getParameter("description");

            ArtworkEntity ae = new ArtworkEntity();
            ae.setAuthor(author);
            ae.setDescription(description);
            ae.setName(name);
            ae.setExpositionId(1);
            System.out.println(ae);
            am.create(ae);
            
            System.out.println(name);
            
            nextPage = "/";
            

        } else if (servletPath.equals("/delete")) {
            
            ArtworkEntity ae = new ArtworkEntity();
            ae.setId(Long.parseLong(request.getParameter("id")));
           
           am.delete(ae);
            nextPage = "/";
        }

        dispatcher = request.getRequestDispatcher(nextPage);
        dispatcher.forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        try
        {
            processRequest(request, response);
        }
        catch(XQException ex)
        {
            Logger.getLogger(CRUDServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        try
        {
            processRequest(request, response);
        }
        catch(XQException ex)
        {
            Logger.getLogger(CRUDServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo()
    {
        return "Short description";
    }// </editor-fold>
}
